package com.linjuke.childay.biz;

import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.linjuke.childay.biz.query.RegionItemQuery;
import com.linjuke.childay.util.CollectionUtil;
import com.linjuke.childay.util.DateUtil;

public class JSONHelper {

	public static ItemDO json2Item(JSONObject jsonItem) {
		try {
			return json2ItemDO((JSONObject)jsonItem.get("data"));
		} catch (JSONException e) {
			Log.e("parse json error", e.getMessage(), e);
			return null;
		}
	}
	
	private static ItemDO json2ItemDO(JSONObject jsonItem) {
		ItemDO itemDO = new ItemDO();
		try {
			if(jsonItem.has(ItemDO.ID)) {
				itemDO.setId(jsonItem.getLong(ItemDO.ID));
			}
			if(jsonItem.has(ItemDO.USER_ID)) {
				itemDO.setUserId(jsonItem.getLong(ItemDO.USER_ID));
			}
			if(jsonItem.has(ItemDO.PRICE)) {
				itemDO.setPrice(jsonItem.getLong(ItemDO.PRICE));
			}
			if(jsonItem.has(ItemDO.DESCRIPTION)) {
				itemDO.setDescription(jsonItem.getString(ItemDO.DESCRIPTION));
			}
			if(jsonItem.has(ItemDO.PHONE)) {
				itemDO.setPhone(jsonItem.getString(ItemDO.PHONE));
			}
			if(jsonItem.has(ItemDO.CAT_ID)) {
				itemDO.setCatId(jsonItem.getInt(ItemDO.CAT_ID));
			}
			if(jsonItem.has(ItemDO.STATUS)) {
				itemDO.setStatus(jsonItem.getInt(ItemDO.STATUS));
			}
			if(jsonItem.has(ItemDO.LONGITUDE)) {
				itemDO.setLongitude(jsonItem.getInt(ItemDO.LONGITUDE));
			}
			if(jsonItem.has(ItemDO.LATITUDE)) {
				itemDO.setLatitude(jsonItem.getInt(ItemDO.LATITUDE));
			}
			if(jsonItem.has(ItemDO.ALTITUDE)) {
				itemDO.setAltitude(jsonItem.getInt(ItemDO.ALTITUDE));
			}
			if(jsonItem.has(ItemDO.DELETED)) {
				itemDO.setDeleted(jsonItem.getInt(ItemDO.DELETED));
			}
			if(jsonItem.has(ItemDO.PIC1)) {
				itemDO.setPic1(jsonItem.getString(ItemDO.PIC1));
			}
			if(jsonItem.has(ItemDO.PIC2)) {
				itemDO.setPic2(jsonItem.getString(ItemDO.PIC2));
			}
			if(jsonItem.has(ItemDO.PIC3)) {
				itemDO.setPic3(jsonItem.getString(ItemDO.PIC3));
			}
			if(jsonItem.has(ItemDO.GMT_CREATE)) {
				itemDO.setGmtCreate(DateUtil.parse(jsonItem.getString(ItemDO.GMT_CREATE)));
			}
			if(jsonItem.has(ItemDO.GMT_MODIFIED)) {
				itemDO.setGmtModified(DateUtil.parse(jsonItem.getString(ItemDO.GMT_MODIFIED)));
			}
		} catch (Exception e) {
			Log.e("item", e.getMessage(), e);
			return null;
		}
		return itemDO;

	}

	public static List<ItemDO> json2Items(JSONObject jsonObject) {
		try {
			if(jsonObject == null) {
				return CollectionUtil.newArrayList();
			}
			
			JSONArray data = jsonObject.getJSONArray("data");
			if (data == null) {
				return CollectionUtil.newArrayList();
			}
			List<ItemDO> items = CollectionUtil.newArrayList();
			for (int len = data.length(), i = 0; i < len; ++i) {
				ItemDO itemDO = json2ItemDO((JSONObject)data.get(i));
				if(itemDO != null) {
					items.add(itemDO);
				}
			}
			
			return items;
		} catch (Exception e) {
			Log.e("item list", e.getMessage(), e);
		}
		return CollectionUtil.newArrayList();
	}
	
	public static RegionItemQuery json2RegionItemQuery(JSONObject jsonObject) {
		RegionItemQuery regionItemQuery = new RegionItemQuery();
		try {
			if(jsonObject == null) {
				return regionItemQuery;
			}
			
			JSONObject data = jsonObject.getJSONObject("regionData");
			if (data == null) {
				return regionItemQuery;
			}
			
			if(data.has(RegionConstant.MIN_LATITUDE)) {
				regionItemQuery.setMinLatitude(data.getInt(RegionConstant.MIN_LATITUDE));
			}
			
			if(data.has(RegionConstant.MIDDLE1_LATITUDE)) {
				regionItemQuery.setMiddle1Latitude(data.getInt(RegionConstant.MIDDLE1_LATITUDE));
			}
			
			if(data.has(RegionConstant.MIDDLE2_LATITUDE)) {
				regionItemQuery.setMiddle2Latitude(data.getInt(RegionConstant.MIDDLE2_LATITUDE));
			}
			
			if(data.has(RegionConstant.MAX_LATITUDE)) {
				regionItemQuery.setMaxLatitude(data.getInt(RegionConstant.MAX_LATITUDE));
			}
			
			if(data.has(RegionConstant.MIN_LONGITUDE)) {
				regionItemQuery.setMinLongitude(data.getInt(RegionConstant.MIN_LONGITUDE));
			}
			
			if(data.has(RegionConstant.MIDDLE1_LONGITUDE)) {
				regionItemQuery.setMiddle1Longitude(data.getInt(RegionConstant.MIDDLE1_LONGITUDE));
			}
			
			if(data.has(RegionConstant.MIDDLE2_LONGITUDE)) {
				regionItemQuery.setMiddle2Longitude(data.getInt(RegionConstant.MIDDLE2_LONGITUDE));
			}
			
			if(data.has(RegionConstant.MAX_LONGITUDE)) {
				regionItemQuery.setMaxLongtitude(data.getInt(RegionConstant.MAX_LONGITUDE));
			}
			
			if(data.has(RegionConstant.PAGE_NO)) {
				regionItemQuery.setPageNo(data.getInt(RegionConstant.PAGE_NO));
			}
			
			if(data.has(RegionConstant.PAGE_SIZE)) {
				regionItemQuery.setPageSize(data.getInt(RegionConstant.PAGE_SIZE));
			}
			
		} catch (Exception e) {
			Log.e("get RegionItemQuery error ", e.getMessage(), e);
		}
		return regionItemQuery;
	}
	
	public static void main(String[] args) {
		List<ItemDO> list = CollectionUtil.newArrayList();
		if(CollectionUtil.isEmpty(list)) {
			System.out.println("empty");
		}
		else {
			System.out.println("not empty");
			System.out.println(list.get(0));
		}
	}
	
}
